$("#login-info").hide();
$("#login-in-btn").hide();

$(document).ready(function () {

    $("#file-menu a").click(function (e) {
        e.preventDefault();
        $("#file-menu .active").removeClass("active");
        $(this).addClass("active");
    })

    $("#file-menu #myhome").click(function (e) {
        e.preventDefault();
        $("#file-table").bootstrapTable("removeAll");
        $.ajax({
            url: serviceInfo.ip + "getSharedFile",
            success: function (res) {
                $("#file-table").bootstrapTable("load", res);
                console.log(res);
                $("#file-remove").hide();
                $("#file-repeat").hide();
                $("#file-upload").hide();
                $("#file-edit").hide();
            },
            error : function(x,e){
                alert(e);
            },
            beforeSend : function(e){
                showloading();
            },
            complete : function ( e){
                hideloading();
            }
        
        })
    })

    $("#file-menu #myfile").click(function (e) {

        if (!isLogin()) {
            window.location.href = "./login.html"
            return;
        }

        var username = getCookie("userName");

        e.preventDefault();
        $($(this).child).removeClass("glyphicon-folder-close").addClass("glyphicon-folder-open");
        $("#file-table").bootstrapTable("removeAll");

        //     var url = serviceInfo.ip + "getSharedFileUser"

        $.ajax({
            url: serviceInfo.ip + "getFileByUser?username=" + username,
            success: function (res) {
                $("#file-table").bootstrapTable("load", res);
                console.log(res);
                $("#file-upload").show();
                $("#file-remove").show();
                $("#file-edit").show();
                $("#file-repeat").hide();
            },
            error : function(x,e){
                alert(e);
            },
            beforeSend : function(e){
                showloading();
            },
            complete : function ( e){
                hideloading();
            }
        })
    })

    $("#file-menu #mytrash").click(function (e) {

        if (!isLogin()) {
            window.location.href = "./login.html"
            return;
        }
        var username = getCookie("userName");
        e.preventDefault();
        $("#file-table").bootstrapTable("removeAll");
        $.ajax({
            url: serviceInfo.ip + "getDeletedFileUser?username=" + username,
            success: function (res) {
                $("#file-table").bootstrapTable("load", res);
                console.log(res);
                $("#file-remove").hide();
                $("#file-repeat").show();
                $("#file-edit").hide();
                $("#file-upload").hide();
            },
            error : function(x,e){
                alert(e);
            },
            beforeSend : function(e){
                showloading();
            },
            complete : function ( e){
                hideloading();
            }
        })
    })


    //首页数据加载
    $("#file-menu #myhome").trigger("click");

    //设置IP
    $("#now-ip").text(returnCitySN.cip)
    $("#now-city").text(returnCitySN.cname);

    //设置时间

    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var seperator2 = ":";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }

        var ss = date.getSeconds();
        if (ss < 10) {
            ss = "0" + ss;
        }

        var mm = date.getMinutes();
        if (mm < 10) {
            mm = "0" + mm;
        }
        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
            " " + date.getHours() + seperator2 + mm +
            seperator2 + ss;
        return currentdate;
    }

    setInterval(function () {
        $("#now-time").text(getNowFormatDate());
    }, 1000)


    //是否已经登录
    if (isLogin()) {
        $("#login-info").show();
        $("#login-info #login-user #login-user-name").text(getCookie("userName"));
        $("#login-in-btn").hide();
    } else {
        $("#login-info").hide();
        $("#login-in-btn").show();
    }

    //登出事件
    $("#login-out").click(function (e) {
        e.preventDefault();
        delCookie("userName");
        delCookie("isLogin");
        window.location.href = "./index.html"
    })

    $("#login-in-btn").click(function () {
        window.location.href = "./login.html"
    })

    //文件上传组件初始化
    $('#file-upload-input').fileinput({
        theme: 'fa',
        language: 'zh',
        uploadExtraData: {},
        elErrorContainer: "#errorBlock",
        browseLabel: '',
        removeLabel: '',
        browseIcon: '<i class="glyphicon glyphicon-folder-open"></i>',
        removeIcon: '<i class="glyphicon glyphicon-remove"></i>',
        removeTitle: '删除',
        // uploadUrl : 'http://118.89.161.29:10086/upload'
        showUpload: false,
    });


    $("#file-info-submit").click(function () {
        $("#file-upload-form #username").val(getCookie('userName'));
        var data = new FormData($('#file-upload-form')[0]);
        $.ajax({
            url: serviceInfo.ip + "upload",
            type: 'POST',
            data: data,
            dataType: 'JSON',
            cache: false,
            processData: false,
            contentType: false,
            error : function(x,e){
                alert(e);
            },
            beforeSend : function(e){
                showloading();
            },
            complete : function ( e){
                hideloading();
            },
            success : function(res){
                if (res.result) {
                    $("#file-upload-form  #file-form-reset").trigger('click');
                    $("#myModal-close").trigger('click');
                    $("#file-menu #myfile").trigger('click');
                } 
            }
        });

    })

    $("#file-download").click(function (e) {
        e.preventDefault();
        var datas = $("#file-table").bootstrapTable('getAllSelections');

        datas.forEach(function (element, index) {
            var url = encodeURI(serviceInfo.ip + "download?filename=" + element.filename + "&filepath=" + element.filepath);
            // $.ajax({
            //     url:url,
            //     success : function(res){
            //        console.log(res);
            //     }
            // })
            window.open(url);
        });
    })


    $("#file-remove").click(function (e) {
        e.preventDefault();
        var datas = $("#file-table").bootstrapTable('getAllSelections');

        datas.forEach(function (element, index) {
            var url = encodeURI(serviceInfo.ip + "delete?fileid=" + element.id);
            if (!window.confirm("确定将" + element.filename +" 放到回收站吗?")) return;
            $.ajax({
                url: url,
                success: function (res) {
                    $("#file-menu #myfile").trigger('click');
                    console.log(res);
                },
                error : function(x,e){
                    alert(e);
                },
                beforeSend : function(e){
                    showloading();
                },
                complete : function ( e){
                    hideloading();
                }
            })
        });
    })

    $("#file-repeat").click(function (e) {
        e.preventDefault();
        var datas = $("#file-table").bootstrapTable('getAllSelections');
        datas.forEach(function (element, index) {
            var url = encodeURI(serviceInfo.ip + "repealfile?fileid=" + element.id);
            $.ajax({
                url: url,
                success: function (res) {
                    console.log(res);
                },
                error : function(x,e){
                    alert(e);
                },
                beforeSend : function(e){
                    showloading();
                },
                complete : function ( e){
                    hideloading();
                }
            })
        });
    })




    //文本编辑窗口初始化


    var E = window.wangEditor
    var editor = new E('#editor')
    editor.customConfig.menus = [
        'head',
        'bold', //粗体
        'italic', //斜体
        'justify', // 对齐方式
        'underline',
        'undo', // 撤销
        'redo' // 重复
    ]
    editor.create()

    //  设置高度


    //工具栏按钮点击事件初始化
    $("#file-edit").click(function (e) {
        e.preventDefault();
        $('#myModa2').modal('show')


        //获取文本内容 并且填充进去

        var datas = $("#file-table").bootstrapTable('getAllSelections');
        var url = encodeURI(serviceInfo.ip + "getTextContentByFileId?fileid=" + datas[0].id);
        $.ajax({
            url: url,
            success: function (res) {
                console.log(res);
                editor.txt.html(res.result);
            },
            error : function(x,e){
                alert(e);
            },
            beforeSend : function(e){
                showloading();
            },
            complete : function ( e){
                hideloading();
            }
        })
    })


    //保存
    $("#file-edit-save").click(function () {
        var datas = $("#file-table").bootstrapTable('getAllSelections');
        // var url = encodeURI(serviceInfo.ip + "getTextContentByFileId?fileid=" + datas[0].id);
        var id = datas[0].id
        var content = editor.txt.text()
        var obj = {
            fileid: id,
            content: content
        }
        $.ajax({
            url: serviceInfo.ip + "saveEditedFile",
            type: "post",
            data: JSON.stringify(obj),
            contentType: "application/json",
            success: function (res) {
                if (res.result == "true") {
                    alert("保存成功")
                } else {
                    alert("保存失败 请检查日记");
                }
            },
            error: function (e) {
                alert("错误！！");
            },
            beforeSend : function(e){
                showloading();
            },
            complete : function ( e){
                hideloading();
            }
        });
    });


});